<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script type="text/javascript" src="../common/jquery-3.2.1.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            var manager = getCurrentManager();
            if (manager != null) {
                window.top.location.href = "main.html";
                return;
            }
            $.ajax({
                url: "../manager/init/canInitAdmin",
                data: {},
                success: function (ajaxResult) {
                    if (ajaxResult.success) {
                        $("#initAdminFormDiv").show();
                        $("#loginFormDiv").hide();
                    } else {
                        $("#initAdminFormDiv").hide();
                        $("#loginFormDiv").show();
                    }
                },
                error: function () {
                    alert("请求服务失败");
                }
            });
        });

        function getCurrentManager() {
            var manager = null;
            $.ajax({
                url: "../manager/main/current",
                data: {},
                async: false,
                success: function (ajaxResult) {
                    if (ajaxResult.success) {
                        manager = ajaxResult.manager;
                    } else {
                        alert("失败，原因：" + ajaxResult.message);
                    }
                },
                error: function () {
                    alert("请求服务失败");
                }
            });
            return manager;
        }

        function initAdmin() {
            var form = $("#initAdminForm");
            $.ajax({
                url: form.attr("action"),
                data: form.serialize(),
                success: function (ajaxResult) {
                    if (ajaxResult.success) {
                        alert("设置成功，进行登陆")
                        window.top.location.reload();
                    } else {
                        alert("失败，原因：" + ajaxResult.message);
                    }
                },
                error: function () {
                    alert("请求服务失败");
                }
            });
        }

        function login() {
            var form = $("#loginForm");
            $.ajax({
                url: form.attr("action"),
                data: form.serialize(),
                success: function (ajaxResult) {
                    if (ajaxResult.success) {
                        window.top.location.href = "main.html";
                    } else {
                        alert("失败，原因：" + ajaxResult.message);
                    }
                },
                error: function () {
                    alert("请求服务失败");
                }
            });
        }

    </script>
</head>
<body>
<br><br><br><br><br><br><br><br>
<div id="initAdminFormDiv" style="display:none;">
    <form id="initAdminForm" action="../manager/init/initAdmin">
        <table width="100%">
            <tr>
                <td align="right" width="50%">
                    首次使用需设置超级管理员&nbsp;&nbsp;&nbsp;
                </td>
                <td width="50%"></td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    管理员id：
                </td>
                <td>
                    <input type="text" name="managerId">
                </td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    名称：
                </td>
                <td>
                    <input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    密码：
                </td>
                <td>
                    <input type="password" name="password">
                </td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    <input type="button" onclick="initAdmin()" value="提交">
                </td>
                <td width="50%"></td>
            </tr>
        </table>
    </form>
</div>
<div id="loginFormDiv" style="display:none;">
    <form id="loginForm" action="../manager/main/login">
        <table width="100%">
            <tr>
                <td align="right" width="50%">
                    登陆&nbsp;&nbsp;&nbsp;
                </td>
                <td width="50%"></td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    管理员id：
                </td>
                <td width="50%">
                    <input type="text" name="managerId">
                </td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    密码：
                </td>
                <td width="50%">
                    <input type="password" name="password">
                </td>
            </tr>
            <tr>
                <td align="right" width="50%">
                    <input type="button" onclick="login()" value="提交">
                </td>
                <td width="50%"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>